<?php 
	namespace app\admin\controller;
	use think\Db;
	class News extends Base{
		public function newsList(){			
//			$data = Db::name("service_team")->select();
//			$count = count($data);
			$data = Db::name('news')->order("news_id desc")->paginate(5,false, [ 'type'=>'Bootstrap2' ] );
			// $data = $data->toArray();
			
			$d = $data->all();
			foreach ($d as $k=>$v) {
				$v['news_time'] = date("Y-m-d",$v['news_time']);
				$data[$k] = $v;
			}
			// echo "<pre>";
			// var_dump($data);echo "</pre>";die;
			$this->assign("num",$data->getCurrentPage()*5-5);
			$this->assign("news_data",$data);
			return $this->fetch();
		}
		public function addNews(){
			if($_POST){
				$e = 0;//没上传错误
				$data = array(
					'news_title'=>$_POST['news_title'],
					'news_content'=>$_POST['news_content'],
					'news_time'=>strtotime($_POST['news_time'])
				);
				$id = Db::name("news")->insert($data,false,true);
				if($id)$this->redirect("newsList");
			}			
			return $this->fetch();
		}
		
		public function editNews($news_id){
			$data = Db::name('news')->where("news_id",$news_id)->find();	
			$data['news_time'] = date("Y-m-d",$data['news_time']);
			$this->assign("news",$data);
			return $this->fetch();
		}
		public function saveEdit(){
			if($_POST){
				$e = 0;//没上传错误
				$nid = $_POST['news_id'];
				$data = array(
					'news_title'=>$_POST['news_title'],
					'news_content'=>$_POST['news_content'],
					'news_time'=>strtotime($_POST['news_time'])
				);
				$id = Db::name("news")->where("news_id",$nid)->update($data);
				$this->redirect("newsList");
			}
		}

		public function delNews($news_id){
			Db::name("news")->where("news_id",$news_id)->delete();
			$this->redirect("newsList");
		}
	}
	
?>